package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.dto.IdentityAuthAuditForm;
import cn.com.chengmandian.bike.dto.IdentityAuthDto;
import cn.com.chengmandian.bike.dto.IdentityAuthForm;
import cn.com.chengmandian.bike.dto.IdentityMemberStudentForm;
import cn.com.chengmandian.bike.model.EbIdentityAuth;
import cn.com.chengmandian.bike.model.vo.IdentityAuthVo;
import cn.com.chengmandian.bike.service.IEbIdentityAuthService;
import cn.com.chengmandian.core.db.pojo.PageData;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 电单车运营-身份认证审核 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-09-18
 */
@Api(tags = "身份认证审核")
@RestController
@RequestMapping("/identity/auth")
public class EbIdentityAuthController {

    @Autowired
    private IEbIdentityAuthService identityAuthService;

    @ApiOperation("用户身份认证审核")
    @GetMapping("/pages")
    public AjaxResult<PageData<IdentityAuthVo>> pages(IdentityAuthDto dto) {
        IPage<IdentityAuthVo> page = identityAuthService.getPage(dto, AuthUtil.getAgentUser().getId());
        return AjaxResult.success(PageData.build(page));
    }

    @ApiOperation("用户最新身份审核")
    @GetMapping("/newest")
    public AjaxResult<EbIdentityAuth>  newestAuth(IdentityAuthDto dto) {
        Assert.notNull(dto.getIdentityId(), "缺少身份ID");
        return AjaxResult.success(identityAuthService.newestAuth(dto.getIdentityId(), AuthUtil.getAgentUser().getId()));
    }

    @ApiOperation("添加认证信息")
    @PostMapping("/add/auth")
    public AjaxResult<EbIdentityAuth> addAuth(@RequestBody IdentityAuthForm form) {
        Assert.notNull(form.getIdentityId(), "缺少证件类型");
        Assert.isTrue(StrUtil.isNotBlank(form.getCardNo()), "缺少身份证号");
        Assert.isTrue(IdcardUtil.isValidCard(form.getCardNo()), "身份证号格式错误");
        Assert.isTrue(StrUtil.isNotBlank(form.getName()), "缺少认证姓名");

        return AjaxResult.success(identityAuthService.addAuth(form, AuthUtil.getAgentUser().getId()));
    }

    @ApiOperation("用户学生信息")
    @PostMapping("/member/student")
    public AjaxResult memberStudent(IdentityMemberStudentForm form) {
        Assert.notNull(form.getIdentityAuthId(), "缺少身份认证审核ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getWxStudentcheckCode()), "缺少用户授权查询code");
        return AjaxResult.success(identityAuthService.memberStudent(form, AuthUtil.getAgentUser().getId(), LocalMapUtil.getAgentInfo()));
    }

    @ApiOperation("再次提交审核")
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody IdentityAuthAuditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.notNull(form.getIdentityId(), "缺少证件类型");
        Assert.isTrue(StrUtil.isNotBlank(form.getCardNo()), "缺少身份证号");
        Assert.isTrue(IdcardUtil.isValidCard(form.getCardNo()), "身份证号格式错误");
        Assert.isTrue(StrUtil.isNotBlank(form.getName()), "缺少认证姓名");

        identityAuthService.audit(form);
        return AjaxResult.success();
    }
}
